//
//  WBNetworkManager+Extension.swift
//  weibo
//
//  Created by HanXujie on 2017/4/20.
//  Copyright © 2017年 thinker. All rights reserved.
//

import Foundation

// MARK: - 封装新浪微博的请求
extension WBNetworkManager{
    
    
    
    /// 加载微博数据字典数组
    ///
    /// - Parameters:
    ///   - since_id: 返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0
    ///   - max_id: 返回ID小于或等于max_id的微博，默认为0
    ///   - completion: 完成回调[list：微博字典数组是否成功]
    func statusList(since_id: Int64 = 0 ,max_id: Int64 = 0 ,completion:@escaping (_ list: [[String: Any]]?, _ isSuccess: Bool)->()){
        
        // URL
        let url = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        // 参数字典
        let params = ["since_id" : "\(since_id)", "max_id" : "\(max_id > 0 ? max_id - 1 : 0)"]
        
        // 请求数据
        tokenRequest(method: .GET, URLString: url, parameters: params) { (json, isSuccess) in
            //            print(json ?? "")
            
            let list = json as? [String: Any]
            let result = list?["statuses"]
            completion(result as? [[String : Any]], isSuccess)
            
        }
    }
    
    
    /// 微博未读数据的数量
    ///
    /// - Parameter completion: 未读数量的回调
    func unreadCount(completion:@escaping (_ count: Int)->()) {
        guard let uid = uid else {
            return
        }
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
        let params = ["uid": uid]
        
        tokenRequest(URLString: urlString, parameters: params) { (json, isSuccess) in
            
            
            let dic = json as? [String :Any]
            let count = dic?["status"] as? Int
            completion(count ?? 0)
        }
        
    }
}
